package com.woniuxy.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageInfo;
import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.dto.system.LoginDTO;
import com.woniuxy.entity.dto.system.LoginUserDTO;
import com.woniuxy.entity.dto.system.UserDTO;
import com.woniuxy.entity.dto.system.UserToShowHeaderMsgDto;
import com.woniuxy.entity.po.system.User;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.service.UserService;
import com.woniuxy.util.*;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.service
 * @Author: Quan
 * @Date: 2025/3/27 9:29
 */
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

	private UserMapper userMapper;
	private PageHelperUtil pageHelperUtil;
	private MinioUtils minioUtils;
	private RedisTemplate<String, Object> redisTemplate;
	/**
	 * 1、根据用户进行查询
	 * 为空：返回
	 * 不为空，继续进行
	 * 2、对user密码进行加密后进行比对
	 * 不成功，返回null
	 * 成功，继续进行
	 * 3、创建jwt
	 */
	@Override
	public LoginDTO login(User user) throws JOSEException {
		User userInfo = userMapper.queryByUsernameForLogin(user);
		if (userInfo == null) {
			return null;
		}

		// 此处加盐值
		String md5PWD = SecureUtil.md5(user.getPassword() + "1a2b3c4d");
		if (!userInfo.getPassword().equals(md5PWD)) {
			return null;
		}

		Map<String, Object> userMap = new HashMap<>();
		userMap.put("id", userInfo.getId());
		userMap.put("account", userInfo.getUsername());
		String jwt = JWTUtil.createJWT(userMap);

		// 将jwt存入数据库中
		redisTemplate.opsForValue().set("JWT:" + userInfo.getId(), jwt, 20, TimeUnit.DAYS);
		LoginDTO loginDTO = new LoginDTO();
		loginDTO.setJwt(jwt);
		loginDTO.setUserInfo(new LoginUserDTO(userInfo));

		return loginDTO;
	}

	@Override
	public ResponseData<?> queryAll(PageParam<UserDTO> pageParam) {
		PageInfo<?> pageInfo = pageHelperUtil.getPageInfo(pageParam, userMapper::queryAll,UserDTO::new);
		return ResponseData.ok(pageInfo);
	}

	@Override
	public void deleteUser(Long id) {
		userMapper.deleteUser(id);
	}

	@Override
	public void addUser(User user) {
		user.setPassword(SecureUtil.md5(user.getPassword() + "1a2b3c4d"));
		userMapper.addUser(user);
	}

	@Override
	public void batchDeleteUser(List<Long> ids) {
		userMapper.batchDeleteUser(ids);
	}

	@Override
	public UserDTO queryIdInfo(Long id) {
		return new UserDTO(userMapper.queryIdInfo(id));
	}

	@Override
	public void updateUser(UserDTO userDTO) {
		userDTO.setPassword(SecureUtil.md5(userDTO.getPassword() + "1a2b3c4d"));
		userMapper.updateUser(new User(userDTO));
	}

	@Override
	public void deptClean(Long deptId) {
		userMapper.deptClean(deptId);
	}
	@Override
	public ResponseData<?> uploadImg(MultipartFile file) throws Exception {
		String imgSrc = minioUtils.uploadFile(file);
		return ResponseData.ok(imgSrc);
	}

	/**
	 * @Description: 查询用户的对应头像的图片地址
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/7
	 */
	@Override
	public ResponseData<?> getImgSrc() throws ParseException {
		// 获取当前请求的上下文
		ServletRequestAttributes servletRequestAttributes =
				(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = servletRequestAttributes.getRequest();

		String token = request.getHeader("token");
		User user = userMapper.queryUserById(JWTUtil.getUserId(token));

		return ResponseData.ok(new UserToShowHeaderMsgDto(user));
	}
}
